<?php

/**
 * Booking View for RoomBooking Component
 * 
 * @version 1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license		GNU/GPL
 */
// no direct access

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class RoomBookingViewBooking extends JView {

    /**
     * Booking view display method
     * @return void
     * */
    function display($tpl = null) {
        $mainframe = &JFactory::getApplication();

        $id = JRequest::getVar('cid');
        $item = & $this->get('Data');
        $model = $this->getModel();
        if ($model->exists($id) || $id == 0) {
            $this->assignRef('item', $item);
            if (!isset($item->id_room)) {
                $id_room = JRequest::getVar('id_room');
                $item->id_room = $id_room;
            }

            $modelRoom = &$this->getModel('room');
            $modelBuilding = & $this->getModel('building');

            $this->assignRef('roomName', $modelRoom->getRoomName($item->id_room));
            $this->assignRef('cost', $modelRoom->getRoomCost($item->id_room));

            $id_building = $modelRoom->getIdBuildOfRoom($item->id_room);
            $times = $modelBuilding->getBuildOpenCloseTime($id_building);
            $minTime = $times->opening;
            $maxTime = $times->closing;
            $this->assignRef('maxTime', $maxTime);
            $this->assignRef('minTime', $minTime);

            $we_opening = $modelBuilding->getWEOpening($id_building);
            $this->assignRef('we_opening', $we_opening);

            $params = &JComponentHelper::getParams('com_roombooking');
            $edit_gid = $params->get('edit_group');
            $this->assignRef('edit_gid', $edit_gid);

            $start = JRequest::getVar('start');
            $end = JRequest::getVar('end');
            if (isset($start) && isset($end)) {
                $this->item->start = $start;
                $this->item->end = $end;
            }
            $userID_logg = JFactory::getUser()->id;
            $user_gid = JFactory::getUser()->gid;

            //if no edit privileges redirect to default layout
            if ($this->item->id_user != $userID_logg && $user_gid < $edit_gid && $this->item->id != 0)
                $this->setLayout('default');

            if ($userID_logg == 0 && $this->getLayout() == 'form') {
                $uri = JFactory::getURI();
                $return = $uri->toString();

                $url = 'index.php?option=com_user&view=login';
                $url .= '&return=' . base64_encode($return);

                $mainframe->redirect($url, JText::_('FIRSTLOGIN'));
            }
        }
        else
            $this->setLayout('notify');
        
        
        //user list
        $db = & JFactory::getDBO();
        $query = 'SELECT id, name FROM #__users;';
        $db->setQuery($query);
        $users = $db->loadObjectList();
        $user_list = JHTML::_('select.genericlist', $users, 'id_user', 'class=" inputbox"', 'id', 'name', JFactory::getUser()->id);
        $this->assignRef('user_list', $user_list);
        
        //debug
        $user =& JFactory::getUser();
        $this->assignRef('usertype', $user->usertype);
        //debug
        
//        echo 'minhbkpro-'.JFactory::getUser()->id;
        
        parent::display($tpl);
    }

    /**
     * Method to get name Building of a room
     * @return	string
     */
    function getBuildOfRoom($id_room) {
        $modelRoom = & $this->getModel('room');
        $modelBuilding = & $this->getModel('building');
        $id_building = $modelRoom->getIdBuildOfRoom($id_room);
        $build = $modelBuilding->getBuildName($id_building);

        return $build;
    }
}
